package com.jason.cloud.engine.share.driver.project.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jason.cloud.engine.share.driver.enterprise.bean.EnterpriseBean;
import com.jason.cloud.engine.share.driver.enterprise.service.EnterpriseService;
import com.jason.cloud.engine.share.driver.pack.bean.PackBean;
import com.jason.cloud.engine.share.driver.pack.service.PackService;
import com.jason.cloud.engine.share.driver.project.bean.ProjectBean;
import com.jason.cloud.engine.share.driver.project.service.ProjectService;
import com.jason.cloud.engine.share.driver.role.bean.RoleBean;
import com.jason.cloud.engine.share.driver.user.bean.UserBean;
import com.jason.cloud.engine.share.driver.user.util.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Controller
@RequestMapping("/project")
public class ProjectController {

    private static final Logger logger = LoggerFactory.getLogger(ProjectController.class);

    @Resource
    private ProjectService projectService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private PackService packService;

    /**
     * 列举
     * @return
     */
    @RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject getProjectLimitByEnterpriseId(@RequestParam int page, @RequestParam int limit, @RequestParam String enterpriseId) {

        //加载所有
        List<ProjectBean> projectBeanList = this.projectService.getProjectLimitByEnterpriseId((page - 1 ) * limit, limit, enterpriseId.trim());
        int count = this.projectService.getProjectCountByEnterpriseId(enterpriseId);

        //返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", count);

        //遍历数据
        JSONArray jsonArrayProject = new JSONArray();
        ProjectBean projectBean = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(Iterator itr = projectBeanList.iterator(); itr.hasNext();) {
            projectBean = (ProjectBean)itr.next();
            JSONObject json = new JSONObject();
            json.put("id", projectBean.getId());
            json.put("alias", projectBean.getAlias());
            json.put("name", projectBean.getName());
            json.put("remarks", projectBean.getRemarks() == null?"--":projectBean.getRemarks());
            json.put("type", projectBean.getType());
            json.put("startDate", simpleDateFormat.format(projectBean.getStartDate()));
            json.put("stopDate", simpleDateFormat.format(projectBean.getStopDate()));
            json.put("createDate", simpleDateFormat.format(projectBean.getCreateDate()));
            jsonArrayProject.add(json);
        }
        jsonObject.put("data", jsonArrayProject);
        return jsonObject;
    }

    /**
     * 新建
     * @param json
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public @ResponseBody JSONObject insertProject(@RequestBody JSONObject json) {

        JSONObject jsonObject = new JSONObject();

        String name = json.getString("name");
        String alias = json.getString("alias");
        String remarks = json.getString("remarks");
        String type = json.getString("type");
        String enterpriseId = json.getString("enterpriseId");
        Date startDate = json.getDate("startDate");
        Date stopDate = json.getDate("stopDate");

        //企业对象
        EnterpriseBean enterpriseBean = this.enterpriseService.queryEnterpriseById(enterpriseId.trim());

        ProjectBean projectBean = new ProjectBean();
        projectBean.setName(name);
        projectBean.setAlias(alias);
        projectBean.setRemarks(remarks);
        projectBean.setType(type);
        projectBean.setEnterpriseId(enterpriseBean.getId());
        projectBean.setStartDate(startDate);
        projectBean.setStopDate(stopDate);
        projectBean.setCreateDate(new Date());

        this.projectService.insertProject(projectBean);

        //返回时数据
        jsonObject.put("status", "200");
        jsonObject.put("msg", "项目创建成功");

        return jsonObject;
    }

    /**
     * 检查项目配额数
     * @param enterpriseId
     * @return
     */
    @RequestMapping(value = "/checkProjectQuota/{enterpriseId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    public @ResponseBody JSONObject checkProjectQuota(@PathVariable String enterpriseId) {
        JSONObject jsonObject = new JSONObject();

        //获取企业对象
        EnterpriseBean enterpriseBean = this.enterpriseService.queryEnterpriseById(enterpriseId.trim());

        //获取资费对象
        PackBean packBean = this.packService.queryPackById(enterpriseBean.getPackId().trim());
        int count_project_quota = packBean.getProjects();

        //计数用户
        int count = this.projectService.getProjectCountByEnterpriseId(enterpriseId.trim());

        if (count < count_project_quota) {
            jsonObject.put("status", 200);
            jsonObject.put("msg", "");
        } else {
            jsonObject.put("status", 201);
            jsonObject.put("msg", "企业项目配额已用完、请联系管理员。");
        }
        return jsonObject;
    }
}
